<?php
include('../../includes/classes/core.php');
 

$action	= $_REQUEST['act'];
$user_id	= $_SESSION['USERID'];
$error	= '';
$data	= '';
$prinn_kart='';

switch ($action) {
	case 'get_edit_page':
		$wareHouse	= $_REQUEST['id'];
		$page 		= GetPage($wareHouse);
		$data		= array('page'	=> $page);

		break;
	case 'get_add_page':
        $page		= GetReservePage();
        $data 		= array('page'	=> $page);

		break;
	case 'packing_list':
		$wareHOuse_id	= $_REQUEST['id'];
		$page		= GetPackingPage($wareHOuse_id);
		$data 		= array('page'	=> $page);

		break;
	case 'save_comment':
		 mysql_query("
		     
		 
		                     UPDATE `warehouse`
                    LEFT JOIN a_letter	ON a_letter.id=`warehouse`.a_letter_id
                    LEFT JOIN d_letter  ON d_letter.id=`warehouse`.d_letter_id
                    SET 
                    	d_letter.`comment`='$_REQUEST[text]',
                        a_letter.`comment`='$_REQUEST[text]'
                    WHERE (`warehouse`.`id`='$_REQUEST[id]'); 
		 ");
		
	 break;
	 
	 case 'comment':
	     $query= mysql_fetch_array(mysql_query("SELECT IFNULL(a_letter.`comment`,d_letter.`comment`)
	     FROM warehouse
	     LEFT JOIN a_letter ON a_letter.id = warehouse.a_letter_id
	     LEFT JOIN d_letter ON d_letter.id = warehouse.d_letter_id
	     WHERE warehouse.id='$_REQUEST[id]'"));
	     $data[comment]=$query[0];
	     break;
		
	case 'unknown_return':	     
	    mysql_query("UPDATE `warehouse` SET warehouse.`actived`='1' WHERE (`warehouse`.`id`='$_REQUEST[id]');");	     
	    break;
	case 'disable':
	    if ($_REQUEST[tab]=="in5") {
	        mysql_query("UPDATE `warehouse` SET warehouse.`actived`='1' WHERE (`warehouse`.`id`='$_REQUEST[id]');");
	    }else {
		DisableWarehouse($_REQUEST['id']);
	    }
		break;
	case 'get_reserve_list':
		$user_id = $_SESSION['USERID'];
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];
	    $quantity = $_REQUEST['q'];
	    $cartridge = $_REQUEST['ct'];
	    $localiD = $_REQUEST['localiD'];


	    $data = array(
	    		"aaData"	=> array()
	    );
	    $cartridgeID = mysql_query("SELECT	`production`.`id`
			    					FROM	`production`
			    					WHERE	`production`.`name` ='$cartridge'");
	    $cartridgeID 		= mysql_fetch_array( $cartridgeID );
	    for ( $i = 0 ; $i < $quantity; $i++ )
	    {
	    	mysql_query("INSERT INTO	`warehouse_detail`
	    					(`warehouse_id`,`prod_id`, `user_id`,`actived`)
	    				VALUES
	    					($localiD,'$cartridgeID[0]', $user_id, '1' )");
	    	mysql_query("INSERT INTO	`warehouse_detail_history`
				    		(`warehouse_id`,`prod_id`, `user_id`,`actived`)
				    	VALUES
				    		($localiD,'$cartridgeID[0]', $user_id, '1' )");
	    }
	    $rsql = mysql_query("SELECT		`warehouse_detail`.`id`,
	    								`production`.`name`,
	    								`warehouse_detail`.`barcode`
	    					FROM		`warehouse_detail`
	    					LEFT JOIN	`production` ON `production`.`id` =  `warehouse_detail`.`prod_id`
	    					WHERE		`warehouse_detail`.`warehouse_id` = '$localiD'");

	    while ( $aRow = mysql_fetch_array( $rsql ) )
	    {
	    	$row = array();
	    	for ( $i = 0 ; $i < $count ; $i++ )
	    	{
	    		// General output
	    		$row[] = $aRow[$i];
	    	}
	    	$data['aaData'][] = $row;
	    }

		break;
	case "move_unknown":
	    $sq=mysql_query("
                    UPDATE `warehouse`
                    LEFT JOIN a_letter	ON a_letter.id=`warehouse`.a_letter_id
                    LEFT JOIN d_letter  ON d_letter.id=`warehouse`.d_letter_id
                    SET warehouse.`actived`='2',
                    	d_letter.`comment`='$_REQUEST[text]',
                        a_letter.`comment`='$_REQUEST[text]'
                    WHERE (`warehouse`.`id`='$_REQUEST[id]'); ");
	  //  $error=mysql_affected_rows($sq);
	    
	    break;
	    
    case 'get_list':
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];
	    $status = $_REQUEST['status'];
		$statusArr = array();

		if($status == 2){
			$statusArr[] = 2;
			$statusArr[] = 3;
			$st="AND `warehouse`.`actived` = 1";
		}else if($status == 4){
			//$prinn_cart='AND NOT ISNULL(warehouse.a_letter_id)';
			$statusArr[] = 4;
			$statusArr[] = 4;
			$st="AND `warehouse`.`actived` = 1";
		}else if($status == 5){
			$statusArr[] = 5;
			$statusArr[] = 5;
			$st="AND IFNULL(d_letter.`status`,a_letter.`status`)NOT IN (5,6) AND `warehouse`.`actived` = 1";
		}else if($status == 6){
			$statusArr[] = 6;
			$statusArr[] = 6;
		}else if($status == 7){
			$statusArr[] = 5;
			$statusArr[] = 5;
			$st="AND IFNULL(d_letter.`status`,a_letter.`status`) IN (5,6) AND `warehouse`.`actived` = 1";
		}else if($status == 8){
			$statusArr[0] = "0 OR TRUE";
			$statusArr[1] = "0 OR TRUE";
			$st="AND warehouse.actived=2";
		}else{
			$statusArr[] = 0;
			$statusArr[] = 1;
			$st="AND `warehouse`.`actived` = 1";
		}


	    $rResult = mysql_query(" SELECT  	`warehouse`.`id`,
											`warehouse`.`end_date`,
	                                         IFNULL(a_letter.end_date, d_letter.end_date),
												CASE WHEN NOT ISNULL(`warehouse`.`d_letter_id`) THEN CONCAT( '№: დ-',`warehouse`.`d_letter_id`)
													 WHEN NOT ISNULL(`warehouse`.`a_letter_id`) THEN CONCAT( '№: ა-',`warehouse`.`a_letter_id`)
													 ELSE CONCAT( '№: R-',`warehouse`.`id`)
												END as `letter`,
									      		CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') AS `client`,
									      		COUNT(warehouse_detail.id),
                                                 priority_catalog.`name` as `priority`,     
												`warehouse_status`.`name`
									FROM  		`warehouse` as `warehouse`
									LEFT JOIN 	client_objects  ON `client_objects`.`id` = `warehouse`.`client_object_id`
									LEFT JOIN 	d_letter  ON `d_letter`.`id` = `warehouse`.`d_letter_id`
									LEFT JOIN 	a_letter  ON `a_letter`.`id` = `warehouse`.`a_letter_id`
									LEFT JOIN 	client ON client.id = `client_objects`.`client_id`
	        			            LEFT JOIN   postal_codes ON client_objects.postal_code_id = postal_codes.id
                                    LEFT JOIN   districts ON districts.id = postal_codes.district_id
	        						LEFT JOIN   priority_catalog ON priority_catalog.id=IFNULL(d_letter.`priority`,a_letter.`priority`)
									JOIN 	`warehouse_detail` ON `warehouse_detail`.`warehouse_id` = `warehouse`.`id` and warehouse_detail.actived=1
									LEFT JOIN 	`warehouse_status` ON `warehouse`.`status` = `warehouse_status`.`id`
	    							WHERE 		(`warehouse`.`status` = $statusArr[0] OR `warehouse`.`status` = $statusArr[1]) $st 
									GROUP BY 	`warehouse`.`id`
	        ORDER BY $_REQUEST[sort_type] (IFNULL(d_letter.`priority`,a_letter.`priority`)) DESC, warehouse.end_date ASC

	    		");

		$data = array(
			"aaData"	=> array()
		);

		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count; $i++ )
			{
				// General output
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}


			$data['aaData'][] = $row;
		}

        break;
    case 'get_local_id':
		$local_id = GetLocalID();
		$data = array('local_id' => $local_id);

        break;
	case 'clear_db':
		//ClearDB();

		break;
	case 'save_reserve':
		$wareHOuse_id	= $_REQUEST['id'];
		$objectName		= $_REQUEST['clObj'];
		$objectName		= GetClientID($objectName);
		$end_date		= $_REQUEST['date'];

		saveWarehouse($wareHOuse_id,$user_id,$objectName,$end_date);

		break;
	case 'get_client_id':
		$client_name	= $_REQUEST['cn'];
		$client_id		= GetClientID2($client_name);

		$data = array('client_id' => $client_id);

		break;

	case 'recive':
		$wareHOuse_id	= $_REQUEST['id'];
		checkStatus($wareHOuse_id);

		break;
	case 'send_producing':
		$wareHOuse_id	= $_REQUEST['id'];
		sendManufacturing($wareHOuse_id);

		break;
	case 'check':
		$data		= array('message'	=> check());

		break;
	case 'pack':
		$wareHOuse_id	= $_REQUEST['id'];
		$type			= $_REQUEST['type'];

		$part		    = json_decode( '['.$_REQUEST['parts'].']' );
		packing($wareHOuse_id, $part, $type);

		break;
	case 'inner_pack':
		$wareHOuseDetail_id	= $_REQUEST['id'];
		$part		    = json_decode( '['.$_REQUEST['parts'].']' );
		inner_packing($wareHOuseDetail_id,$part);

		break;

	case 'change_location':
		$barcode 	= $_REQUEST['barcode'];
		$location 	= $_REQUEST['location'];
		$condition 	= $_REQUEST['condition'];
		$prod_id 	= (int) substr($barcode, 5, 4);
		$c_data = date('Y-m-d H:i:s');
		if( GetBarcodeStatus($barcode) ){
			if (CheckExist($barcode) >= 1) {
				mysql_query("	UPDATE 	`reserve`
								SET 	`reserve`.`object_id` = $location,
										`reserve`.`production_condition` =  $condition
								WHERE	`reserve`.`barcode` = '$barcode' ");
			}else {
				mysql_query("	INSERT INTO reserve
							   		(object_id, production_id, barcode, production_condition, quantity, create_date)
								VALUES
									($location, $prod_id, '$barcode', $condition, 0, '$c_data')");
			}
		}else{
			$error = 'შტრიხკოდი არასწორია!';
		}

		break;
	case 'check_barcodes':
		$wareHOuse_id	= $_REQUEST['id'];
		CheckBarcodes($wareHOuse_id);
		break;
		case 'get_warehouse_id' :
			$barcode	= $_REQUEST['barcode'];
			$data		= array('warehouseID'	=> GetWarehouseId($barcode));
			$page 			= GetPage($wareHouseDetID, $data, $barcode);
			break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);

function CheckBarcodes($wareHOuse_id){
	$result = mysql_fetch_assoc( mysql_query("	SELECT		warehouse.id
												FROM		warehouse
												LEFT JOIN	warehouse_detail ON warehouse_detail.warehouse_id = warehouse.id
												WHERE		warehouse.id =  '$wareHOuse_id' AND warehouse_detail.barcode = '' && `warehouse`.`actived` = 1 && warehouse_detail.`actived` = 1") );
	if( $result != ''){
		global $error;
		$error = 'ყველა კატრიჯი არ არის შეყვანილი!';
	}

}

function GetBarcodeStatus($barcode) {
	$result = mysql_fetch_assoc( mysql_query("	SELECT	`barcodes`.`actived` as `actived`
												FROM	`barcodes`
												WHERE	`barcodes`.`code` = '$barcode'") );
	return ($result['actived'] == '1');
}

function CheckExist($barcode) {
	$result = mysql_query("	SELECT `reserve`.`id`
							FROM `reserve`
							WHERE `reserve`.`barcode` = '$barcode'");

	return mysql_num_rows($result);

}

function ClearDB()
{
	$req = mysql_query("SELECT		`id`
						FROM		`warehouse_detail`
						WHERE		`warehouse_id` NOT IN (SELECT `id` FROM `warehouse`)");

	while( $res = mysql_fetch_assoc($req)){
		mysql_query("DELETE
					FROM
						`warehouse_detail`
					WHERE
						`id` = '$res[id]'");
	}
	while( $res = mysql_fetch_assoc($req)){
		mysql_query("DELETE
					 FROM
						`warehouse_detail_history`
					WHERE
						`id` = '$res[id]'");
	}
}
function GetWarehouseId($barcode){
	$rResult = mysql_query("SELECT		warehouse.id AS w_id,
			warehouse_detail.id as wd_id,
			warehouse.producing_status
			FROM		warehouse
			LEFT JOIN	warehouse_detail ON warehouse_detail.warehouse_id = warehouse.id
			WHERE		warehouse.producing_status = 2 && warehouse_detail.barcode = '$barcode' AND warehouse_detail.actived=1
			ORDER BY	warehouse_detail.warehouse_id DESC
			LIMIT 1");
			$aRow = mysql_fetch_array( $rResult );
			if($aRow != ''){
			return 	$aRow[w_id];
	}else{
	global $error;
	$error = 'შტრიხკოდი არასწორია!';
			}

}
function GetLocalID(){
	GLOBAL $db;
	$local_id = $db->increment('warehouse');
	return $local_id;
}
function DisableWarehouse($warehous_id){

	$a_letter_id = mysql_fetch_assoc(mysql_query("SELECT 	warehouse.a_letter_id
												  FROM		warehouse
												  WHERE		warehouse.id = $warehous_id"));
	$d_letter_id = mysql_fetch_assoc(mysql_query("SELECT 	warehouse.d_letter_id
												  FROM		warehouse
												  WHERE		warehouse.id = $warehous_id"));
	mysql_query("	UPDATE 		a_letter
					SET 		actived = 0
					WHERE 		id = $d_letter_id[d_letter_id] ");

	mysql_query("	UPDATE 		a_letter
					SET 		actived = 0
					WHERE 		id = $a_letter_id[a_letter_id] ");

	mysql_query("	UPDATE 	warehouse_detail
					SET		warehouse_detail.actived = 0
					WHERE	warehouse_detail.warehouse_id = $warehous_id");

	mysql_query("	UPDATE 	warehouse_detail_history
					SET		actived = 0
					WHERE	warehouse_id = $warehous_id");


	mysql_query("	UPDATE 		warehouse
					SET        	warehouse.actived = 0
					WHERE		warehouse.id = $warehous_id");

}
function GetPackingPage($wareHOuse_id){
	$data = '
	<div id="dialog-form">
		<fieldset>
			<legend>შეფუთვა</legend>
			<div class="inner-table">
				    <div id="dt_example" class="ex_highlight_row">
				        <div id="container" class="overhead_container">
				        	<div id="button_area">
				        	</div>
				            <div id="dynamic">
								<table class="display" id="packing_list">
					                    <thead>
					                        <tr id="datatable_header">
					                            <th>ID</th>
					                            <th style="width: 100%">ტიპი</th>
					                            <th style="width: 80px">#</th>
					                        </tr>
					                    </thead>
					                    <thead>
					                        <tr class="search_header">
					                            <th class="colum_hidden">
					                            	<input type="text" name="search_idee" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_typee" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                            </th>
					                        </tr>
					                    </thead>
				                </table>
				            </div>
				        </div>
				    </div>
				</div>
		</fieldset>
    </div>
	<div>
		<input type="hidden" style="" name="search_id" class="search_init"  id="wareHouseID_packing" value="'.$wareHOuse_id.'"/>
	</div>';

	return $data;
}
function saveWarehouse($wareHOuse_id,$user_id,$objectName,$end_date){
	$c_data = date('Y-m-d H:i:s');
	mysql_query("INSERT INTO	`warehouse`
					(`id`, `user_id`, `a_letter_id`, `client_object_id`, `start_date`,`end_date`, `status`)
				VALUES
					($wareHOuse_id, $user_id,NULL, $objectName  ,'$c_data','$end_date',1)");
}

function GetClientID($objectName){
	$csql = mysql_query("SELECT		client_objects.id
									FROM 		`client_objects`
									LEFT JOIN 	`client` ON `client`.`id` = `client_objects`.`client_id`
									WHERE		CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') = '". $objectName."'");

	$csql 		= mysql_fetch_array( $csql );
	return $csql[0];
}

function GetClientID2($client_name){

		$res = mysql_fetch_assoc(mysql_query("	SELECT		`client`.`id`
				FROM		`client_objects`
				LEFT JOIN	`client` ON `client`.`id` = `client_objects`.`client_id`
				WHERE		CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') = '$client_name'"));
		return $res['id'];

}

function checkStatus($wareHOuse_id){
	$csql = mysql_query("SELECT		`status`
						 FROM		`warehouse`
						 WHERE		id = '$wareHOuse_id'");
	$csql 		= mysql_fetch_array( $csql );
	if($csql[0] == '0'){
		mysql_query("
				UPDATE	`warehouse`
				SET		`status` = 1
				WHERE	id = '$wareHOuse_id'");
	}
}

function sendManufacturing($wareHOuse_id){
	checkStatus($wareHOuse_id);
	$csql = mysql_query("SELECT		`status`
						 FROM		`warehouse`
						 WHERE		id = '$wareHOuse_id'");
	$csql 		= mysql_fetch_array( $csql );
	if($csql[0] == '1'){
		mysql_query("UPDATE		`warehouse`
					SET			`status`	= 2
					WHERE		`id`		= '$wareHOuse_id'");
		///////////////
		mysql_query("UPDATE		`warehouse`
		SET						`producing_status`	= 0
		WHERE					`id`		= '$wareHOuse_id'");
	}
}

function check(){
	$rResult = mysql_query("	SELECT  	CASE WHEN `warehouse`.`status` = 0 THEN CONCAT('მიღებულია შეკვეთა ', client.`name`,'(',client_objects.`name`, ') -საგან')
											END AS 'notify'
											FROM  	`warehouse` as `warehouse`
											LEFT JOIN client_objects  ON `client_objects`.`id` = `warehouse`.`client_object_id`
											LEFT JOIN d_letter  ON `d_letter`.`id` = `warehouse`.`d_letter_id`
											LEFT JOIN a_letter  ON `a_letter`.`id` = `warehouse`.`a_letter_id`

											RIGHT JOIN client ON client.id = `client_objects`.`client_id`
											RIGHT JOIN `warehouse_detail` ON `warehouse_detail`.`warehouse_id` = `warehouse`.`id`
											LEFT JOIN `warehouse_status` ON `warehouse`.`status` = `warehouse_status`.`id`
											WHERE			IF( NOT ISNULL(`warehouse`.`d_letter_id`), d_letter.priority, a_letter.priority )	= 3 && ( NOT ISNULL(`warehouse`.`d_letter_id`) || NOT ISNULL(`warehouse`.`a_letter_id`) ) && `warehouse`.`status` in (0,4)
							UNION
								SELECT  	CASE WHEN `warehouse`.`status` = 0 THEN CONCAT('მიღებულია შეკვეთა ', client.`name`,'(',client_objects.`name`, ') -საგან')
							     				WHEN `warehouse`.`status` = 4 THEN CONCAT('შესაფუთად მზადაა ', client.`name`,'(',client_objects.`name`, ')–ის შეკვეთა')
											END AS 'notify'
								FROM  		`warehouse` as `warehouse`
								LEFT JOIN 	client_objects  ON `client_objects`.`id` = `warehouse`.`client_object_id`
								LEFT JOIN 	d_letter  ON `d_letter`.`id` = `warehouse`.`d_letter_id`
								LEFT JOIN 	a_letter  ON `a_letter`.`id` = `warehouse`.`a_letter_id`
								RIGHT JOIN 	client ON client.id = `client_objects`.`client_id`
								RIGHT JOIN 	`warehouse_detail` ON `warehouse_detail`.`warehouse_id` = `warehouse`.`id`
								LEFT JOIN 	`warehouse_status` ON `warehouse`.`status` = `warehouse_status`.`id`
								WHERE		`warehouse`.`status` in (0,4)
								GROUP BY `warehouse`.`id`
							LIMIT	1");
	$aRow = mysql_fetch_array( $rResult );
	return $aRow[0];
}

function packing($wareHOuse_id,$part='', $type){
	$csql = mysql_query("SELECT		`status`
						FROM		`warehouse`
						WHERE		id = '$wareHOuse_id'");
	$csql 		= mysql_fetch_array( $csql );
	if($csql[0] == '4'){
		mysql_query("UPDATE	`warehouse`
					SET		`status` = 5
					WHERE	id = '$wareHOuse_id'");
		$c_data = date('Y-m-d H:i:s');
		foreach($part as $partID){
			mysql_query("UPDATE	`warehouse`
								`warehouse`.`packed` = $partID
								WHERE	id = '$wareHOuse_id'");
			/* აქ რეზერვებში ჩამოკლება უნდა მოხდეეს */
		}
		if( $type == '1' ){
			$aletter = mysql_query("SELECT		`warehouse`.`a_letter_id`
									FROM		`warehouse`
									WHERE		`warehouse`.`id` = '$wareHOuse_id'");
			$aletter 		= mysql_fetch_array( $aletter );
			if($aletter[0] != ''){
				mysql_query("UPDATE	`a_letter`
							SET		`status` = 4
							WHERE	`id` 	= '$aletter[0]'");

				$c_data = date('Y-m-d H:i:s');

				mysql_query("UPDATE	`a_letter`
							SET		`fact_end_date` = '$c_data'
							WHERE	`id` 	= '$aletter[0]'");
			}
		}else{
			$dletter = mysql_query("SELECT		`warehouse`.`d_letter_id`
									FROM		`warehouse`
									WHERE		`warehouse`.`id` = '$wareHOuse_id'");
			$dletter 		= mysql_fetch_array( $dletter );
			if($dletter[0] != ''){
				mysql_query("UPDATE	`d_letter`
							SET		`status` = 4
							WHERE	`id` 	= '$dletter[0]'");

				$c_data = date('Y-m-d H:i:s');

				mysql_query("UPDATE	`d_letter`
							SET		`fact_end_date` = '$c_data'
							WHERE	`id` 	= '$dletter[0]'");
			}
		}

	}
}

function inner_packing($wareHOuseDetail_id,$part){
	$c_data = date('Y-m-d H:i:s');
	global $user_id;
	foreach($part as $partID){
		/* აქ რეზერვებში ჩამოკლება უნდა მოხდეეს */
		mysql_query("INSERT INTO	`warehouse_producing`
						(`packer_id`,`warehouse_detail_id`,`cartridge_part_id`,`date`)
					VALUES
						($user_id,$wareHOuseDetail_id,$partID,'$c_data')");
		mysql_query("INSERT INTO	`warehouse_producing_history`
						(`packer_id`,`warehouse_detail_id`,`cartridge_part_id`,`date`)
					VALUES
						($user_id,$wareHOuseDetail_id,$partID,'$c_data')");
	}
}

function GetReservePage(){
	$data = '
	<!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="რეზერვის შექმნა">
    	<!-- aJax -->
	</div>
	<div id="dialog-form">
		<fieldset>
			<legend>ძირითადი ინფორმაცია</legend>
			<table width="80%" class="dialog-form-table">
				<tr align="center">
					<th><label for="client_object">კლიენტი</label></th>
					<th>
						<div class="seoy-row" id="client_object_seoy">
							<input type="text" id="client_object" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" />
							<button id="client_object_btn" class="combobox">client_object</button>
						</div>
					</th>
				</tr>
				<tr align="center">
					<th><label for="cartridge_model">მოდელი</label></th>
					<th>

						<div class="seoy-row" id="cartridge_model_seoy">
							<input type="text" id="cartridge_model" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" />
							<button id="cartridge_model_btn" class="combobox">cartridge_model</button>
						</div>
					</th>
				</tr>
				<tr align="center">
					<th><label for="end_date">დას. გეგმიური დრო</label></th>
					<th>
							<input type="text" id="end_date"/>
					</th>
				</tr>
				<tr align="center">
					<th><label for="client_object">რაოდენობა</label></th>
					<th>
							<input type="text" maxlength="2" id="quantity" value="" />
					</th>
				</tr>
			</table>
		</fieldset>
		<fieldset>
			<legend>შესრულება</legend>
			<div class="inner-table">
				    <div id="dt_example" class="ex_highlight_row">
				        <div id="container" class="overhead_container">
				        	<div id="button_area">

				        	</div>
				            <div id="dynamic">
				                <table class="display" id="reserve_list">
				                    <thead>
				                        <tr id="datatable_header">
				                            <th>ID</th>
				                            <th style=" width : 100%">მოდელი</th>
											<th style=" width : 100%">შტრიხკოდი</th>
											<th  style=" width : 30px;"></th>

				                        </tr>
				                    </thead>
				                    <thead>
			                        <tr class="search_header">
				                            <th class="colum_hidden">
				                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
				                            </th>
				                            <th>
				                            	<input type="text" name="search_prod" value="ფილტრი" class="search_init" />
				                            </th>
											<th>
												<input type="text" name="search_comm" value="ფილტრი" class="search_init" />
											</th>
											<th ></th>
				                        </tr>
				                    </thead>
				                </table>
				            </div>
				        </div>
				    </div>
				</div>
		</fieldset>
		<!-- ID -->
    </div>
    ';

	return $data;
}

function GetPage($wareHouse){
	$rResult = mysql_query("SELECT		`warehouse`.`end_date` as `date`,
										`warehouse`.`a_letter_id`  as `a_letter`,
										CONCAT(`client`.`name`,'(',`client_objects`.`name`, ')') AS `client`,
										`warehouse`.`status` AS `status`,
										`client`.`name` AS `client_name`,
										`warehouse`.`d_letter_id` as `d_letter`
							FROM		`warehouse`
							RIGHT JOIN	(`client_objects` RIGHT JOIN `client` ON `client`.`id` = `client_objects`.`client_id`) ON `client_objects`.`id` = `warehouse`.`client_object_id`
							WHERE `warehouse`.`id` = $wareHouse");
	$status = 1;
	$aRow = mysql_fetch_array( $rResult );
	$letter		= 'ა - '.$aRow['a_letter'];
	$end_time	= $aRow['date'];
	$client		= $aRow['client'];
	if( $aRow['d_letter']  != ''){
		$letter		= 'დ - '.$aRow['d_letter'];
		$status = 2;
	}elseif( $aRow['a_letter']  != ''){
		$letter		= 'ა - '.$aRow['a_letter'];
		$status = 1;
	}else {
		$letter		= 'R - '.$wareHouse;
		$status = 0;
	}
	$comment='';
	if ($_REQUEST[tab]=="in5") {
	    $query= mysql_fetch_array(mysql_query("SELECT IFNULL(a_letter.`comment`,d_letter.`comment`)
                        FROM warehouse
                        LEFT JOIN a_letter ON a_letter.id = warehouse.a_letter_id
                        LEFT JOIN d_letter ON d_letter.id = warehouse.d_letter_id
                        WHERE warehouse.id='$_REQUEST[id]'"));
		$comment="<div class='clear'></div><fieldset> <textarea id='comment_text' style='width: 99%; height:100px'>$query[0]</textarea></fieldset>";
	}

	$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ძირითადი ინფორმაცია</legend>
				<table width="80%" class="dialog-form-table" cellpadding="10px" >
					<tr align="center" >
						<th>
							<label for="client_object">ბარათი № <span style="border-bottom: 1px solid #000; padding: 0 30px; margin-left: 50px;">'.$letter.'</span></label>
						</th>
					</tr>
					<tr align="center" >
						<th>
							<label for="client_object">დასრულების თარიღი<span style="border-bottom: 1px solid #000; padding: 0 30px; margin-left: 50px;">'.$end_time.'</span></label>
						</th>
					</tr>
					<tr align="center">
						<th colspan="2">
							<label for="client_object">კლიენტი<span style="border-bottom: 1px solid #000; padding: 0 30px; margin-left: 50px;">'.$client.'</span></label>
						</th>
					</tr>
				</table>
			</fieldset>
			'.$comment.'				    
			<div class="clear"></div>
			<fieldset>
		    	<legend>კატრიჯები</legend>
				 <div class="produse">
				    <div class="inner-table">
					    <div id="dt_example" class="ex_highlight_row">
					        <div id="container" class="overhead_container">
					        	<div id="button_area">
									<input style="display:inline; " id="add_barcode_input" type="text" maxlength="15"/>
									<button id="add_barcode"  style="margin-left: 40px" >შტრიხკოდის ბეჭდვა</button>
									<button id="delete_button_list">წაშლა</button>

					        	</div>
					            <div id="dynamic">
					                <table class="display" id="prod_list" >
					                    <thead>
						                        <tr id="datatable_header">
													<th>ID</th>
						                            <th style=" width : 40px;"></th>
						                            <th  style=" width : 100%">შტრიხკოდი </th>
													<th  style=" width : 100%">სერიული ნომერი </th>
													<th  style=" width : 100%">მოდელი</th>
													<th  style=" width : 30px;"></th>
													  <th class="check">#</th>
						                        </tr>
					                    </thead>
										<thead>
						                        <tr class="search_header">
							                            <th class="colum_hidden">
														<input type="hidden" name="" value="ფილტრი" class="search_init" />
							                            </th>
														<th>
														<input type="hidden" name="" value="ფილტრი" class="search_init" />
														</th>
														<th>
															<input type="text" name="" value="ფილტრი" class="search_init" />
							                            </th>
							                            <th>
							                            	<input type="text" name="" value="ფილტრი" class="search_init" />
							                            </th>
														<th>
															<input type="text" name="" value="ფილტრი" class="search_init" />
														</th>
														<th>
														</th>
														<th>
														</th>
							                     </tr>
					                    </thead>
					                </table>
					            </div>
					        </div>
					    </div>
					</div>
				</div>
			</fieldset>
		<!-- ID -->
    </div>
		<div class="inner-table" >
		<input type="hidden" style="" name="search_id" class="search_init"  id="wareHouseClient" value="'.$aRow['client_name'].'"/>
	</div>
	<div class="inner-table" >
		<input type="hidden" style="" name="search_id" class="search_init"  id="wareHouseStatus" value="'.$aRow['status'].'"/>
	</div>
	<div class="inner-table" >
		<input type="hidden" style="" name="search_id" class="search_init"  id="letterStatus" value="'.$status.'"/>
	</div>
	<div class="inner-table" >
		<input type="hidden" style="" name="search_id" class="search_init"  id="wareHouse" value="'.$wareHouse.'"/>
	</div>';
	return $data;
}
?>